package com.utils
{
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	
	public class ExternalInterfaceUtil
	{
		private static var _externalInterfaceUtil:ExternalInterfaceUtil;
		public function ExternalInterfaceUtil()
		{
			if(!_externalInterfaceUtil) _externalInterfaceUtil = this;
		}
		
		public static function get instance():ExternalInterfaceUtil
		{
			if(!_externalInterfaceUtil) _externalInterfaceUtil = new ExternalInterfaceUtil();
			return _externalInterfaceUtil;
		}
		
		/**
		 * 给js发送消息 
		 * @param functionName 函数名
		 * @param params 参数
		 * @param 返回发送结果
		 */		
		public function sendToJS(functionName:String,...params):*
		{
			if(ExternalInterface.available)
			{
				return ExternalInterface.call(functionName,params);
			}
			return false;
		}
		
		/**
		 * 接收js消息
		 * @param functionName 函数名
		 * @param callback 回调函数(接收js的数据)
		 */		
		public function receiveFromJS(functionName:String,callback:Function):void
		{
			if(ExternalInterface.available){
				ExternalInterface.addCallback(functionName, callback);
			}
		}
		
		
		
		
		
		
	}
}

